require 'find'

module Find
	def match(*paths)
	    matched = []
	    find(*paths) { |path| matched << path if yield path }
	    return matched
	  end
	  module_function :match
	end	

Find.match("./") do |p|
  if File.file? p
    open(p)  do |f|
  ext = p[-4...p.size]
  if ext && ext.downcase == ".sql"
   l = f.readlines
   grants =nil
   for t in (l.size-6...l.size-1)
       #if ((l[t]!=nil) &&(l[t]="@"))
       if l[t]!=nil
	   if l[t].downcase =~/call.*/ || l[t].downcase =~/grant.*/
	       grants = l[t]
	   end
      end
  end
  #puts "**********************************************************"
  if grants==nil || (grants[0,4].downcase !="call" && grants[0,5].downcase !="grant" )
	      puts "!!! without grant statement:  "+p 
  else
      match = Regexp.compile('\/([[a-zA-Z0-9]*|_*]*)\.sql').match(p.downcase)
      #puts match
      #puts match[1]+" has grant statment, now check if it is correct."
      #puts grants
      checkname=Regexp.compile(match[1]).match(grants.downcase)
      if(!checkname)
            puts "!!! grant statment is not correct: "+p +"---"+ grants
      else
          #puts "grant statment is OK."
      end
   end
   
   end
end
  else
    false
  end
end


